/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.tengfei.common.utils;

import lombok.Data;

/**
 * 返回数据
 *
 * @author Mark sunlightcs@gmail.com
 */

@Data
public class R<T> {
	private static final long serialVersionUID = 1L;

	public static final String SUCCESS_MSG = "操作成功";
	public static final String FAIL_MSG = "操作失败";

	private Integer code;

	private String msg;

	private T data;

	public static <T> R<T> ok() {
		return result(HttpStatus.SUCCESS.getCode(), SUCCESS_MSG, null);
	}

	public static <T> R<T> ok(T data) {
		return result(HttpStatus.SUCCESS.getCode(), SUCCESS_MSG, data);
	}

	public static <T> R<T> ok(String msg) {
		return result(HttpStatus.SUCCESS.getCode(), msg, null);
	}

	public static <T> R<T> ok(String msg, T data) {
		return result(HttpStatus.SUCCESS.getCode(), msg, data);
	}

	public static <T> R<T> fail() {
		return result(HttpStatus.ERROR.getCode(), FAIL_MSG, null);
	}

	public static <T> R<T> fail(String msg) {
		return result(HttpStatus.ERROR.getCode(), msg, null);
	}

	public static <T> R<T> fail(T data) {
		return result(HttpStatus.ERROR.getCode(), FAIL_MSG, data);
	}

	public static <T> R<T> fail(String msg, T data) {
		return result(HttpStatus.ERROR.getCode(), msg, data);
	}

	public static <T> R<T> fail(int code, String msg) {
		return result(code, msg, null);
	}

	public static <T> R<T> result(Integer code, String msg, T data) {
		R<T> r = new R<>();
		r.setCode(code);
		r.setMsg(msg);
		r.setData(data);
		return r;
	}
}
